module.exports = (options, app) => {
    return async function myjwt(ctx, next) {
        try {
            // 白名单，注册和登录是不需要被校验token的
            if (app.config.whiteList.includes(ctx.originalUrl)) {
                await next()
            } else {
                const token = ctx.cookies.get('token')
                let phone
                app.jwt.verify(token, app.config.jwt.secret, (err, decoded) => {
                    console.log(err, decoded)
                    if (err) { // token未提供，token过期
                        throw err
                    } else {
                        phone = decoded.phone
                        console.log(decoded)
                    }
                })
                ctx.request.body = Object.assign({}, ctx.request.body, {
                    phone
                })
                await next()
            }   
        } catch(e) {
            console.log(e)
            ctx.response.body = {
                code: 3, // 3的时候代表需要重新登录
                msg: e.message
            }
        }
    }
};